<?php

class manualImportTask extends sfBaseTask {
  protected function configure() {
    // add your own arguments here
    $this->addArguments(array(
            new sfCommandArgument('csvfile', sfCommandArgument::REQUIRED, 'Stock list CSV file'),
	new sfCommandArgument('from', sfCommandArgument::OPTIONAL, 'Import from a given item number'),
    ));

    $this->addOptions(array(
            new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
            new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
            // add your own options here
    ));
    ini_set('memory_limit', -1);

    $this->namespace        = 'mvm';
    $this->name             = 'manual-import';
    $this->briefDescription = 'Import a CVS stock list';
    $this->detailedDescription = <<<EOF
The [ImportCVSManual|INFO] task does things.
Call it with:

  [php symfony ImportCVSManual|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array()) {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    $vehicle_params = VehicleTable::$conts;

    
    // add your code here
    if (($handle = fopen($arguments['csvfile'],'r')) !== false) {
      $count = 1;
      while(($data = fgetcsv($handle, 1000, ","))!==false ) {
	if($arguments['from'] && $count <=$arguments['from']) {
	$count++;
		continue;
	}
        print_r($data);
        $v = new Vehicle();
        try {
          $bs = BodyStyleTable::getInstance()->findOneByName($data[0]);
          $v->setBodyStyle($bs);

          $trans = array_flip($vehicle_params['vehicle_transmission']);
          $v->setTransmission($trans[$data[1]]);

          $v->setStockId($data[12]);
          $v->setPrice($data[2]);
          $images = array($data[3],$data[4],$data[5],$data[6]);

          $v->setYear($data[7]);
          $v->setMileage($data[8]);

          $color = array_flip($vehicle_params['vehicle_color']);
          $v->setExteriorColor($color[$data[9]]);

          if(preg_match('/[\d]+\s[\d\w]+\s[\d\w]+\s([\w\d]+)$/', $data[10],$match))   $v->setSeries($match[1]);

          $v->setDoors($data[11]);
          $v->setEngine($data[13]);
          $v->setDescription($data[14]);

          

          if($make = MakeTable::getInstance()->findOneByName($data[18])) $v->setMake($make);
          else {
            $mk = new Make();
            $mk->setName($data[18]);
            $mk->save();
            $v->setMake($mk);
          }
          
          if($model = ModelTable::getInstance()->findOneByName($data[16])) $v->setModel($model);
          else {
            $m = new Model();
            $m->setName($data[16]);
            $m->setMake($v->getMake());
            $m->save();
            $v->setModel($m);
          }

          $v->save();

          foreach ($images as $image) {
            if(empty($image)) continue;
            preg_match('/^[\d\w]+\/[\d\w]+\/(.*)$/',$image, $match);

            $vp = new Picture();
            $vp->setFilename($v->getStockId().'_'.$match[1]);
            $vp->setVehicleId($v->getId());
	
	    $thumbnail = new sfthumbnail(800,600);
            $thumbnail->loadFile('http://www.mikevinsen.co.nz/'.$image);
            $thumbnail->save(sfConfig::get('sf_web_dir').'/uploads/vehicle/'.'800x600_'.$v->getStockId().'_'.$match[1],'image/jpeg');
            $thumbnail = new sfthumbnail(400,300);
            $thumbnail->loadFile('http://www.mikevinsen.co.nz/'.$image);
            $thumbnail->save(sfConfig::get('sf_web_dir').'/uploads/vehicle/'.'400x300_'.$v->getStockId().'_'.$match[1],'image/jpeg');

            $thumbnail = new sfthumbnail(90,70);
            $thumbnail->loadFile('http://www.mikevinsen.co.nz/'.$image);
            $thumbnail->save(sfConfig::get('sf_web_dir').'/uploads/vehicle/'.'90x70_'.$v->getStockId().'_'.$match[1],'image/jpeg');

            $vp->save();
          }
          $count++;

        }catch(Exception $e) {
          echo $e->getMessage();
          var_dump($e->getTraceAsString());

        }
      }
      echo $count. " imported\n";
    }
  }
}
